//
//  DecoratingDetailPageVC.m
//  ErWa
//
//  Created by 赵颜彬 on 16/3/10.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DecoratingDetailPageVC.h"
#import "TeamCell.h"
#import "MaterialCell.h"
#import "ProgressCell.h"
#import "UserCheckCell.h"
#import "AFNetworking.h"
#import <UIImageView+WebCache.h>
@interface DecoratingDetailPageVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableDictionary *dataDic;/**< 数据字典 */
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) UIImageView *backImg;/**< 上方背景图 */
@property (nonatomic, strong) UILabel *nameLabel;/**< 名字 */
@property (nonatomic, strong) UILabel *addressLabel;/**< 地址 */
@property (nonatomic, strong) NSMutableDictionary *designerDic;/**< 设计师字典 */
@property (nonatomic, strong) NSMutableDictionary *projectDic;/**< 项目字典 */
@property (nonatomic, strong) NSMutableDictionary *supervisorDic;/**< 工程经理字典 */
@property (nonatomic, strong) NSMutableArray *my_segmentArr;/**< 正在装修数组 */

@end

@implementation DecoratingDetailPageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self dataHandler];
    
    [self createSubviews];
}

/// 数据处理
- (void)dataHandler
{
    NSString *str = @"&app_version=2.1.6&phone_type=iOS&user_token=af9d7efbcf93fba063f4f45fbf442c73-111249-App%5CModels%5CMember&app_channel=appstore&imei=0ABFB620-74E5-4ADF-AA87-C92326CFA2CD&app_type=Owner";
    NSString *urlStr = [NSString stringWithFormat:@"http://v5.owner.mjbang.cn/api/live-shows/new-detail?order_id=%@%@",_detailId, str];
    [AFNetworking GETWithURL:urlStr completion:^(id result) {
        self.dataDic = result[@"data"];
        
        //设计师字典
        self.designerDic =  _dataDic[@"designer"];
        // 项目经理字典
        self.projectDic = _dataDic[@"project_manager"];
        // 工程经理字典
        self.supervisorDic = _dataDic[@"supervisor"];
        // 正在装修数组
        self.my_segmentArr = _dataDic[@"my_segment"];
        
        _backImg.image = [UIImage imageNamed:@"DecoratingBack.jpg"];
        _nameLabel.text = _dataDic[@"member"][@"nickname"];
        _addressLabel.text = _dataDic[@"address"];
        
        [_tableview reloadData];
    }];
    }
/// 创建视图
- (void)createSubviews
{
    self.navigationController.title = @"装修详情";
    // 用户的背景图片
    self.backImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 5)];
    [self.view addSubview:_backImg];
    
    //给背景图片加模糊效果
    UIBlurEffect *b = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:b];
    effectView.frame = _backImg.frame;
    effectView.alpha = 0.8;
    [_backImg addSubview:effectView];
    
    // 用户名Label
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, self.view.frame.size.width - 20, _backImg.frame.size.height / 5)];
    _nameLabel.font = [UIFont systemFontOfSize:19];
    _nameLabel.textColor = [UIColor whiteColor];
    [_backImg addSubview:_nameLabel];
    
    // 地址Label
    self.addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 25 + _nameLabel.frame.origin.y + _nameLabel.frame.size.height + 10, _nameLabel.frame.size.width, _nameLabel.frame.size.height)];
    _addressLabel.textColor = [UIColor whiteColor];
    _addressLabel.font = [UIFont systemFontOfSize:15];
    [_backImg addSubview:_addressLabel];
    
    // 最大的tableView
    self.tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    //注册重用池
    //最基本的cell
    [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    // 装修团队的cell
    [_tableview registerClass:[TeamCell class] forCellReuseIdentifier:@"teamCell"];
    // 材料的cell
    [_tableview registerClass:[MaterialCell class] forCellReuseIdentifier:@"materialCell"];
    // 施工进度的cell
    [_tableview registerClass:[ProgressCell class] forCellReuseIdentifier:@"progressCell"];
    // 业主验收的cell
    [_tableview registerClass:[UserCheckCell class] forCellReuseIdentifier:@"userCheckCell"];
    
    [self.view addSubview:_tableview];
    
    //tableview的头部是背景图片
    _tableview.tableHeaderView = _backImg;
}

// tableView的分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 13;
}

// tableviewCell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 2 || section == 6 || section == 10 ) {
        //水电工程材料 泥水工程材料 油漆工程材料
        return 3;
    }else {
        return 1;
    }
}

// tableView的设置
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 装修团队 包括设计师 项目经理 工程经理
    if (indexPath.section == 0) {
        TeamCell *cell = [tableView dequeueReusableCellWithIdentifier:@"teamCell" forIndexPath:indexPath];
        
        //取消点击cell变灰
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        // 设计师
        cell.designerView.nameLabel.text = _designerDic[@"realname"];
        cell.designerView.professionalLabel.text = @"设计师";
        
        // 项目经理
        cell.project_managerView.nameLabel.text = _projectDic[@"realname"];
        cell.project_managerView.professionalLabel.text = @"项目经理";
        
        //工程经理
        cell.supervisorView.nameLabel.text = _supervisorDic[@"realname"];
        cell.supervisorView.professionalLabel.text = @"工程经理";
        
        return cell;
    }else if (indexPath.section == 1) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        //取消点击cell变灰
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.textLabel.text = @"水电工程";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        return cell;
    }else if (indexPath.section == 5) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        //取消点击cell变灰
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.textLabel.text = @"泥水工程";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        return cell;
    }else if (indexPath.section == 9) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        //取消点击cell变灰
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.textLabel.text = @"油漆工程";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        return cell;
    }else if (indexPath.section == 2){
        MaterialCell *cell = [tableView dequeueReusableCellWithIdentifier:@"materialCell" forIndexPath:indexPath];
        //取消点击cell变灰
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        // 水电工程的材料
        // 水电工程字典
        NSMutableDictionary *dic = _my_segmentArr.firstObject;
        // 材料数组
        NSMutableArray *arr = dic[@"material"];
        if (indexPath.row == 0) {
            cell.typeLabel.text = @"墙砖&地砖";
            cell.nameLabel.text= arr.firstObject[@"name"];
            
        }else if (indexPath.row == 1) {
            cell.typeLabel.text = @"给水材料";
            NSMutableDictionary *dic = [arr objectAtIndex:1];
            cell.nameLabel.text = dic[@"name"];
        }else{
            cell.typeLabel.text = @"电线材料";
            cell.nameLabel.text = arr.lastObject[@"name"];
        }
        return cell;
    } else if (indexPath.section == 6) {
        MaterialCell *cell = [tableView dequeueReusableCellWithIdentifier:@"materialCell" forIndexPath:indexPath];
        //取消点击cell变灰
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        // 泥水工程材料
        // 泥水工程字典
        NSMutableDictionary *dic = [_my_segmentArr objectAtIndex:1];
        // 材料数组
        NSMutableArray *arr = dic[@"material"];
        
        if (indexPath.row == 0) {
            cell.typeLabel.text = @"防水材料";
            cell.nameLabel.text= arr.firstObject[@"name"];
            
        }else if (indexPath.row == 1) {
            cell.typeLabel.text = @"油漆材料";
            NSMutableDictionary *dic = [arr objectAtIndex:1];
            cell.nameLabel.text = dic[@"name"];
            
        }else{
            cell.typeLabel.text = @"厨房铝合金推拉门";
            cell.nameLabel.text = arr.lastObject[@"name"];
            
        }
        return cell;
        
    } else if (indexPath.section == 10) {
        MaterialCell *cell = [tableView dequeueReusableCellWithIdentifier:@"materialCell" forIndexPath:indexPath];
        //取消点击cell变灰
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        // 油漆工程材料
        // 油漆工程字典
        NSMutableDictionary *dic = [_my_segmentArr objectAtIndex:2];
        // 材料数组
        NSMutableArray *arr = dic[@"material"];
        
        if (indexPath.row == 0) {
            cell.typeLabel.text = @"厨房吊顶";
            cell.nameLabel.text= arr.firstObject[@"name"];
            
            
        }else if (indexPath.row == 1) {
            cell.typeLabel.text = @"金牌橱柜";
            NSMutableDictionary *dic = [arr objectAtIndex:1];
            cell.nameLabel.text = dic[@"name"];
            
        }else{
            cell.typeLabel.text = @"马桶&花洒";
            cell.nameLabel.text = arr.lastObject[@"name"];
        }
        return cell;
    } else if (indexPath.section == 3) {
        // 水电工程施工进度
        ProgressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"progressCell" forIndexPath:indexPath];
        //取消点击cell变灰
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        // 水电工程字典
        NSMutableDictionary *dic = _my_segmentArr.firstObject;
        // 进度字典
        NSMutableDictionary *schduleDic = dic[@"schedule"];
        // 进度图片数组
        NSMutableArray *imgArr = schduleDic[@"picture"];
        
        [cell.firstImg sd_setImageWithURL:[NSURL URLWithString:imgArr.firstObject] placeholderImage:[UIImage imageNamed:@"placeHolder.jpg"]];
        NSString *urlStr = [imgArr objectAtIndex:1];
        [cell.secoondImg sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"placeHolder.jpg"]];
        [cell.thirdImg sd_setImageWithURL:[NSURL URLWithString:imgArr.lastObject] placeholderImage:[UIImage imageNamed:@"placeHolder.jpg"]];
        
        return cell;
        
    }else if (indexPath.section == 7) {
        // 泥水工程施工进度
        ProgressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"progressCell" forIndexPath:indexPath];
        //取消点击cell变灰
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        // 泥水工程字典
        NSMutableDictionary *dic = [_my_segmentArr objectAtIndex:1];
        // 进度字典
        NSMutableDictionary *schduleDic = dic[@"schedule"];
        // 进度图片数组
        NSMutableArray *imgArr = schduleDic[@"picture"];
        
        [cell.firstImg sd_setImageWithURL:[NSURL URLWithString:imgArr.firstObject] placeholderImage:[UIImage imageNamed:@"placeHolder.jpg"]];
        NSString *urlStr = [imgArr objectAtIndex:1];
        [cell.secoondImg sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"placeHolder.jpg"]];
        [cell.thirdImg sd_setImageWithURL:[NSURL URLWithString:imgArr.lastObject] placeholderImage:[UIImage imageNamed:@"placeHolder.jpg"]];
        
        return cell;
        
    } else if (indexPath.section == 11) {
        // 油漆工程施工进度
        ProgressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"progressCell" forIndexPath:indexPath];
        //取消点击cell变灰
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        // 油漆工程字典
        NSMutableDictionary *dic = [_my_segmentArr objectAtIndex:2];
        // 进度字典
        NSMutableDictionary *schduleDic = dic[@"schedule"];
        // 进度图片数组
        NSMutableArray *imgArr = schduleDic[@"picture"];
        
        [cell.firstImg sd_setImageWithURL:[NSURL URLWithString:imgArr.firstObject] placeholderImage:[UIImage imageNamed:@"placeHolder.jpg"]];
        NSString *urlStr = [imgArr objectAtIndex:1];
        [cell.secoondImg sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"placeHolder.jpg"]];
        [cell.thirdImg sd_setImageWithURL:[NSURL URLWithString:imgArr.lastObject] placeholderImage:[UIImage imageNamed:@"placeHolder.jpg"]];
        
        return cell;
        
    }else if (indexPath.section == 4) {
        // 水电工程的业主验收
        UserCheckCell *cell = [tableView dequeueReusableCellWithIdentifier:@"userCheckCell" forIndexPath:indexPath];
        //取消点击cell变灰
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        // 水电工程字典
        NSMutableDictionary *dic = _my_segmentArr.firstObject;
        // 进度字典
        NSMutableDictionary *schduleDic = dic[@"schedule"];
        // 评价字典
        NSMutableDictionary *evaluationDic = schduleDic[@"evaluation"];
        
        // 成员字典
        NSMutableDictionary *memberDic = _dataDic[@"member"];
        
        cell.contentLabel.text = evaluationDic[@"comment"];
        cell.nameLabel.text = memberDic[@"nickname"];
        //头像
        NSString *imgStr = memberDic[@"avatar"][@"url"];
        [cell.userImg sd_setImageWithURL:[NSURL URLWithString:imgStr] placeholderImage:[UIImage imageNamed:@"placeHolder.jpg"]];
        
        return cell;
        
    }else if (indexPath.section == 8) {
        //泥水工程业主验收
        UserCheckCell *cell = [tableView dequeueReusableCellWithIdentifier:@"userCheckCell" forIndexPath:indexPath];
        //取消点击cell变灰
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        // 泥水工程字典
        NSMutableDictionary *dic = [_my_segmentArr objectAtIndex:1];
        // 进度字典
        NSMutableDictionary *schduleDic = dic[@"schedule"];
        // 评价字典
        NSMutableDictionary *evaluationDic = schduleDic[@"evaluation"];
        
        // 成员字典
        NSMutableDictionary *memberDic = _dataDic[@"member"];
        
        cell.contentLabel.text = evaluationDic[@"comment"];
        cell.nameLabel.text = memberDic[@"nickname"];
        //头像
        NSString *imgStr = memberDic[@"avatar"][@"url"];
        [cell.userImg sd_setImageWithURL:[NSURL URLWithString:imgStr] placeholderImage:[UIImage imageNamed:@"placeHolder.jpg"]];
        
        return cell;
        
    }else {
        //油漆工程业主验收
        UserCheckCell *cell = [tableView dequeueReusableCellWithIdentifier:@"userCheckCell" forIndexPath:indexPath];
        //取消点击cell变灰
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        // 油漆工程字典
        NSMutableDictionary *dic = [_my_segmentArr objectAtIndex:2];
        // 进度字典
        NSMutableDictionary *schduleDic = dic[@"schedule"];
        // 评价字典
        NSMutableDictionary *evaluationDic = schduleDic[@"evaluation"];
        
        // 成员字典
        NSMutableDictionary *memberDic = _dataDic[@"member"];
        
        cell.contentLabel.text = evaluationDic[@"comment"];
        cell.nameLabel.text = memberDic[@"nickname"];
        //头像
        NSString *imgStr = memberDic[@"avatar"][@"url"];
        [cell.userImg sd_setImageWithURL:[NSURL URLWithString:imgStr] placeholderImage:[UIImage imageNamed:@"placeHolder.jpg"]];
        
        return cell;
    }
}

/// 设置分区头名
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"装修团队";
    }else if (section == 2 || section == 6 || section == 10) {
        return @"材料";
    }else if (section == 3 || section == 7 || section == 11) {
        return @"施工进度";
    }else if (section == 4 || section == 8 || section == 12){
        return @"业主验收";
    }else {
        return 0;
    }
}

// 设置每个分区的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 装修团队部分
    if (indexPath.section == 0 || indexPath.section == 3 || indexPath.section == 7 || indexPath.section == 11 || indexPath.section == 4 || indexPath.section == 8 || indexPath.section == 12) {
        return self.view.frame.size.height / 7;
    }else {
        return 50;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
